package comgutianyu.qihangjiaoyu.fragment;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.fragment.app.Fragment;

import comgutianyu.qihangjiaoyu.R;
import comgutianyu.qihangjiaoyu.activity.LoginActivity;
import comgutianyu.qihangjiaoyu.activity.PersonalInfoActivity;
import comgutianyu.qihangjiaoyu.db.UserDao;
import comgutianyu.qihangjiaoyu.model.User;

public class MineFragment extends Fragment {
    private ImageView ivAvatar;
    private TextView tvUsername, tvLevel;
    private LinearLayout profileLayout;
    private LinearLayout studyStatsLayout;
    private UserDao userDao;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                           Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_mine, container, false);
        
        userDao = new UserDao(getActivity());
        initViews(view);
        return view;
    }

    private void initViews(View view) {
        profileLayout = view.findViewById(R.id.profileLayout);
        ivAvatar = view.findViewById(R.id.avatarImage);
        tvUsername = view.findViewById(R.id.usernameText);
        tvLevel = view.findViewById(R.id.levelText);
        studyStatsLayout = view.findViewById(R.id.studyStatsLayout);
        
        // 设置点击事件
        profileLayout.setOnClickListener(v -> {
            if (isLoggedIn()) {
                // 已登录，跳转到个人信息页面
                startActivity(new Intent(getActivity(), PersonalInfoActivity.class));
            } else {
                // 未登录，跳转到登录页面
                startActivity(new Intent(getActivity(), LoginActivity.class));
            }
        });
    }

    @Override
    public void onResume() {
        super.onResume();
        updateUserInfo();
    }

    private boolean isLoggedIn() {
        return getActivity().getSharedPreferences("user_prefs", getActivity().MODE_PRIVATE)
                .getBoolean("is_logged_in", false);
    }

    private void updateUserInfo() {
        if (isLoggedIn()) {
            // 获取当前登录用户的手机号
            String currentPhone = getActivity().getSharedPreferences("user_prefs", getActivity().MODE_PRIVATE)
                    .getString("current_phone", null);
            
            if (currentPhone != null) {
                // 查询用户信息
                User user = userDao.getUserByPhone(currentPhone);
                if (user != null) {
                    // 显示用户信息
                    tvUsername.setText(user.getNickname());
                    tvLevel.setText(user.getExamIntention());
                    // 如果有头像，设置头像
                    // ivAvatar.setImageResource(...);
                    
                    // 显示学习统计部分
                    studyStatsLayout.setVisibility(View.VISIBLE);
                    return;
                }
            }
        }
        
        // 未登录或获取用户信息失败，显示默认状态
        tvUsername.setText("未登录,请点击登录");
        tvLevel.setText("");  // 清空等级显示
        ivAvatar.setImageResource(R.drawable.default_avatar);
        // 隐藏学习统计部分
        studyStatsLayout.setVisibility(View.GONE);
    }
} 